package de.gwtprime.client.presenter;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.user.ACLManager.PrimeUIElement;
import de.gwtprime.client.widgets.ArticleSectionControlWidgetInterface;
import de.gwtprime.shared.VoteType;
import de.gwtprime.shared.requestFactory.User2Proxy;

public abstract class VoteControlPresenterAbstract implements
		VoteControlPresenter {

	
	protected ClientFactory clientFactory;
	protected ArticleSectionControlWidgetInterface controlWidget;
	
	private User2Proxy owner;
	
	/**
	 * Keep the vote state on the UI
	 */
	VoteType voteType;
	
	/**
	 * Keep track of the vote value
	 */
	Integer voteValue;
	
	/**
	 * 
	 * @param clientFactory
	 * @param controlWidget
	 * @param owner
	 */
	public VoteControlPresenterAbstract(ClientFactory clientFactory, ArticleSectionControlWidgetInterface controlWidget, User2Proxy owner){
		
		this.clientFactory = clientFactory;
		this.controlWidget = controlWidget;
		this.owner = owner;
		
		controlWidget.enableVoteUpBtnFromUser(PrimeUIElement.ArticleSectionVoteUp, owner);
		controlWidget.enableVoteDownBtnFromUser(PrimeUIElement.ArticleSectionVoteDown, owner);
		
		controlWidget.enableVoteCompleteBtnFromUser(PrimeUIElement.ArticleSectionVoteComplete, owner);
	}
	
	
	public Integer setVoteValue(Integer voteValue){
		this.voteValue = voteValue;
		controlWidget.setVoteValue(voteValue);
		return voteValue;
	}
	
	public Integer updateVoteValue(Integer change){
		voteValue += change;
		controlWidget.setVoteValue(voteValue);
		return voteValue;
	}
	
	
	@Override
	public void voteDown() {
		//controlWidget.enableVoteUpBtn(true);
		controlWidget.enableVoteUpBtnFromUser(PrimeUIElement.ArticleSectionVoteUp, owner);
		
		controlWidget.disableVoteDownButtonWithMessage(false, "You already voted down");
		
		controlWidget.setVoteUpBtnActive(false);
		controlWidget.setVoteDownBtnActive(true);
		
		if(voteType == VoteType.UP)
			updateVoteValue(-2); //increase by 2
		else
			updateVoteValue(-1);
		voteType = VoteType.DOWN;
		
		
	}
	
	
	@Override
	public void voteUp() {
		controlWidget.disableVoteUpButtonWithMessage(false, "You already voted up");
		//controlWidget.enableVoteDownBtn(ACLManager.hasUserAccess(clientFactory.getUser(), PrimeUIElement.ArticleSectionVoteDown));
		controlWidget.enableVoteDownBtnFromUser(PrimeUIElement.ArticleSectionVoteDown, owner);
		
		controlWidget.setVoteUpBtnActive(true);
		controlWidget.setVoteDownBtnActive(false);
		
		if(voteType == VoteType.DOWN)
			updateVoteValue(2); //increase by 2
		else
			updateVoteValue(1);
		voteType = VoteType.UP;
		
	}
	
	
	@Override
	public void onVoteComplete() {
		controlWidget.disableVoteCompleteButtonWithMessage(false, "This is already completed");
		controlWidget.setVoteCompleteBtnActive(true);
		
	}
	
	
	protected void setVoteCompleteBtnActiveOnUI() {
		controlWidget.disableVoteCompleteButtonWithMessage(false, "This is already completed");
		// complete vote can not be triggered more then once.
		controlWidget.setVoteCompleteBtnActive(true);
	}
	
	protected void setVoteCompleteBtnInactiveOnUI() {
		controlWidget.enableVoteCompleteBtn(true);
		// complete vote can not be triggered more then once.
		controlWidget.setVoteCompleteBtnActive(false);
	}
	
	protected void setVoteUpBtnActiveOnUI() {
		// Enable the voted down button -> but only if the user has the right
		controlWidget.enableVoteDownBtnFromUser(PrimeUIElement.ArticleSectionVoteDown, owner);
		//controlWidget.enableVoteDownBtn(ACLManager.hasUserAccess(clientFactory.getUser(), PrimeUIElement.ArticleSectionVoteDown));
		
		controlWidget.disableVoteUpButtonWithMessage(false, "You already voted up");
		controlWidget.setVoteUpBtnActive(true);
		controlWidget.setVoteDownBtnActive(false);
	}
	
	protected void setVoteDownBtnActiveOnUI() {
		controlWidget.enableVoteUpBtnFromUser(PrimeUIElement.ArticleSectionVoteUp, owner);
		//controlWidget.enableVoteDownBtn(ACLManager.hasUserAccess(clientFactory.getUser(), PrimeUIElement.ArticleSectionVoteUp));
		controlWidget.disableVoteDownButtonWithMessage(false, "You already voted down");
		controlWidget.setVoteDownBtnActive(true);
		controlWidget.setVoteUpBtnActive(false);
	}


}
